package com.hs.servlet;

import com.alibaba.fastjson.JSON;
import com.hs.dao.DaoImpl.UserDaoImpl;
import com.hs.entity.User;
import com.hs.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        UserDaoImpl userDao = new UserDaoImpl();
        User user = userDao.selectAllByUsernameAndPwd(username, password);

        if (user==null){
            Result success = Result.error();
            resp.getWriter().write(JSON.toJSONString(success));
        }else {
            Result res = Result.success("登录成功!!");
            resp.getWriter().write(JSON.toJSONString(res));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
